package com.atguigu.gmall.item.controller;

import com.atguigu.gmall.item.service.LockService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.concurrent.CountDownLatch;

@RestController
@RequestMapping("/lock")
public class LockTestController {

    @Autowired
    LockService lockService;
    private int i = 0;
    CountDownLatch countDownLatch = new CountDownLatch(1000);

    @GetMapping("/add")
    public void add() {
        countDownLatch.countDown();
        String uuid = lockService.lock();
        i++;
        lockService.unlock(uuid);
    }

    @GetMapping("/result")
    public int result() {
        try {
            countDownLatch.await();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return i;
    }
}
